﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IOSpro.Lib;
using IosDatamodel;
using IOSpro.Models;
namespace IOSpro.Controllers
{
    public class IosUsersController : Controller
    {


        private IosDBEntities db;
        private IUsersBL iblusers;
        public IosUsersController()
        { 
            db=new IosDBEntities();
            iblusers = new UsersBL(this.ModelState);

        }

        public ActionResult Index()
        {
            var users = from a in db.Users select a;
            return View(users);
        }


        public ActionResult RegisteUser()
        {
          
            return View();
        }

        //[HttpPost]
        //public ActionResult RegisteUser(RegisteUser Model)
        //{

        //    if (iblusers.RepeatUser(Model.Email) == false)
        //    {
        //        ViewData["xinxi"] = 0;
        //        //      _errorLog.AddModelError("", "该邮箱已经存在！");

        //    }
        //    else
        //    {
        //        iblusers.SaveUsers(Model.Email, Model.Password);
        //        ViewData["xinxi"] = 1;
        //    }
        //    return View();
        //}

       [HttpPost]
        public ActionResult RegisteUser(String email ,string password)
        {
         
            if (iblusers.RepeatUser(email) == false)
            {
                ViewData["xinxi"] = 0;
          //      _errorLog.AddModelError("", "该邮箱已经存在！");

            }
            else 
            { 
                  iblusers.SaveUsers(email, password);
                  ViewData["xinxi"] = 1;
            }
            return View();  
        }


       public ActionResult LogOnUser()
       {

           return View();
       }

       [HttpPost]
       public ActionResult LogOnUser(String email, string password)
       {

           if (iblusers.LogOnUser(email,password))
           {
               ViewData["xinxi"] = 1;
               if (iblusers.ValidateYesorNot(email)) ViewData["yanzheng"] = 1; else ViewData["yanzheng"] = 0;

     //          _errorLog.AddModelError("", "登录成功！");

           }
           else
           {
               ViewData["xinxi"] = 0;
               ViewData["yanzheng"] = 0;
           }
           return View();
       }

       public ActionResult ValidateQRcode() 
       {





           return View();
       }


       [HttpPost]
       public ActionResult ValidateQRcode(string  email, string validatestring)
       {

           if ((from s in db.Users where s.Verification == validatestring&s.Email==email select s).Count() > 0) ViewData["xinxi"] = 2;

           else
           {
               if (iblusers.GetUsersVerificationCount(validatestring))
               {


                   ViewData["xinxi"] = 1;
                   iblusers.ValidateUser(email, validatestring);
               }
               else
               {
                   // iblusers.ValidateUser(email, validatestring);
                   ViewData["xinxi"] = 0;
               }
           }
           return View();
       }



    }
}
